package com.study.activemq.controller;

import javax.jms.Destination;
import javax.jms.Queue;
import javax.jms.Topic;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTempTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProviderController {
	
	@Autowired // 也可以注入JmsTemplate，JmsMessagingTemplate对JmsTemplate进行了封装
    private JmsMessagingTemplate jmsTemplate;
	
	@Autowired
    private Queue queue;

    @Autowired
    private Topic topic;


	@GetMapping("/test")
	public String test() {
		// 创建自定义队列
		Destination destination = new ActiveMQQueue("test.queue");
		jmsTemplate.convertAndSend(destination, "hello, I am wuyu");

		// 创建自定义主题
		ActiveMQTempTopic activeMQTempTopic = new ActiveMQTempTopic("test.topic");
		jmsTemplate.convertAndSend(activeMQTempTopic, "hello, I am wuyu1");
		return "success";
	}
	
	
	@GetMapping("/queue")
	public String test1() {
		// 向队列中发送消息
		jmsTemplate.convertAndSend(queue, "hello, I am queue");
		return "success";
	}
	
	@JmsListener(destination = "out.queue")
    public void receiveQueue(String text) {
        System.out.println("收到响应的报文为:"+text);
    }
	
	
	@GetMapping("/topic")
	public String test2() {
		// 向主题中发送消息
		jmsTemplate.convertAndSend(topic, "hello, I am topic");
		return "success";
	}
	
	@JmsListener(destination = "out.topic", containerFactory = "jmsListenerContainerTopic")
    public void receivetopic(String text){
        System.out.println("TopicListener: 收到一条信息: " + text);
    }
}
